package com.cgnpc.scp.manager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cgnpc.cud.core.domain.AjaxResult;
import com.cgnpc.scp.common.bean.MyPageResult;
import com.cgnpc.scp.common.pojo.UserInfo;
import com.cgnpc.scp.common.utils.MyPageResultUtil;
import com.cgnpc.scp.common.utils.StringUtil;
import com.cgnpc.scp.manager.mybatis.entity.DataDictGroup;
import com.cgnpc.scp.manager.mybatis.entity.ProcessInfo;
import com.cgnpc.scp.manager.mybatis.mapper.client.DataDictGroupMapper;
import com.cgnpc.scp.manager.service.IDataDictGroupService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import java.util.Date;
import java.util.List;
import java.util.Locale;

@Service
public class DataDictGroupServiceImpl implements IDataDictGroupService {
    private static final Logger LOGGER = LoggerFactory.getLogger(DataDictGroupServiceImpl.class);

    @Autowired
    private DataDictGroupMapper dataDictGroupMapper;
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.setDisallowedFields(new String[]{"admin"});
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public <T extends DataDictGroup> AjaxResult saveOrUpdate(UserInfo loginUserInfo, Locale reqLocale, T reqEntity) {
        try {
            String reqDictGroupKey = reqEntity.getDictGroupKey();
            String reqDictGroupName = reqEntity.getDictGroupName();
            String reqDictGroupDesc = reqEntity.getDictGroupDesc();
            String reqIsValid = reqEntity.getIsValid();
            String reqSystemDataDict = reqEntity.getSystemDataDict();

            if (StringUtil.hasNullOrSpace(
                    reqDictGroupKey, reqDictGroupName, reqIsValid,
                    reqSystemDataDict
            )) {
                return AjaxResult.error("校验失败");
            }

            Date nowDate = new Date();

            // begin 获取登录用户的信息
            String loginUserInfoUserId = loginUserInfo.getUserId();
            String loginUserInfoUserName = loginUserInfo.getUserName();
            // end 获取登录用户的信息

            DataDictGroup dbDataDictGroup = dataDictGroupMapper.selectById(reqDictGroupKey);

            if (dbDataDictGroup == null) {
                // 保存
                DataDictGroup usedDataDictGroup = new DataDictGroup();
                usedDataDictGroup.setDictGroupKey(reqDictGroupKey);
                usedDataDictGroup.setDictGroupName(reqDictGroupName);
                usedDataDictGroup.setDictGroupDesc(reqDictGroupDesc);
                usedDataDictGroup.setCreateTime(nowDate);
                usedDataDictGroup.setCreateUser(loginUserInfoUserId);
                usedDataDictGroup.setCreateUserName(loginUserInfoUserName);
                usedDataDictGroup.setLastUpdateTime(nowDate);
                usedDataDictGroup.setLastUpdateUser(loginUserInfoUserId);
                usedDataDictGroup.setLastUpdateUserName(loginUserInfoUserName);
                usedDataDictGroup.setIsValid(reqIsValid);
                usedDataDictGroup.setSystemDataDict(reqSystemDataDict);

                int saveRows = dataDictGroupMapper.insert(usedDataDictGroup);

                if (saveRows <= 0) {
                    return AjaxResult.error("保存失败");
                } else {
                    return AjaxResult.success("保存成功");
                }
            } else {
                // 修改
                // 修改的时候创建相关数据不变更
                DataDictGroup usedDataDictGroup = new DataDictGroup();
                usedDataDictGroup.setDictGroupKey(reqDictGroupKey);
                usedDataDictGroup.setDictGroupName(reqDictGroupName);
                usedDataDictGroup.setDictGroupDesc(reqDictGroupDesc);
                // usedDataDictGroup.setCreateTime(nowDate);
                // usedDataDictGroup.setCreateUser(loginUserInfoUserId);
                // usedDataDictGroup.setCreateUserName(loginUserInfoUserName);
                usedDataDictGroup.setLastUpdateTime(nowDate);
                usedDataDictGroup.setLastUpdateUser(loginUserInfoUserId);
                usedDataDictGroup.setLastUpdateUserName(loginUserInfoUserName);
                usedDataDictGroup.setIsValid(reqIsValid);
                usedDataDictGroup.setSystemDataDict(reqSystemDataDict);

                int updateRows = dataDictGroupMapper.updateById(usedDataDictGroup);

                if (updateRows <= 0) {
                    return AjaxResult.error("修改失败");
                } else {
                    return AjaxResult.success("修改成功");
                }
            }
        } catch (Exception e) {
            LOGGER.error("save.error;", e);
            throw e;
        }
    }

    @Override
    public   MyPageResult<DataDictGroup> loadByPaging(DataDictGroup reqEntity) {
        // begin 分页开始
        // 第几页
        int pageNum = reqEntity.getPageNum();
        // 每页的数量
        int pageSize = reqEntity.getPageSize();
        // 创建Page对象，将pageNum，pageSize参数传入，必须位于数据库查询数据的语句之前，否则不生效
        com.baomidou.mybatisplus.extension.plugins.pagination.Page<DataDictGroup> page = new Page<>(pageNum, pageSize);

        // mybatis查询方式
        QueryWrapper<DataDictGroup> queryWrapper = new QueryWrapper<>();

        if(!StringUtil.isNullOrSpace(reqEntity.getDictGroupName())){
            queryWrapper.like("dict_group_name",reqEntity.getDictGroupName());
        }
        if(!StringUtil.isNullOrSpace(reqEntity.getDictGroupKey())){
            queryWrapper.like("dict_group_key",reqEntity.getDictGroupKey());
        }
        // 设置排序字段
        // queryExample.setOrderByClause("order_index");

        // 设置查询条件
        // queryExampleCriteria.andCreateTimeIsNotNull();

        // 执行查询
//        List<DataDictGroup> dbEntityList = (List<DataDictGroup>) dataDictGroupMapper.selectList(queryWrapper);
        IPage<DataDictGroup> processInfoIPage = dataDictGroupMapper.selectPage(page, queryWrapper);

        MyPageResult<DataDictGroup> myPageResult = MyPageResultUtil.createMyPageResultPlus(processInfoIPage);
        return myPageResult;
    }

    @Override
    public <T extends DataDictGroup> T queryByDictGroupKey(T reqEntity) {
        String reqDictGroupKey = reqEntity.getDictGroupKey();
        return (T) dataDictGroupMapper.selectById(reqDictGroupKey);
    }
}
